package mypractice;

public class Leetcode121 {
    //超时
    /*public int maxProfit(int[] prices) {
        int i,j,max=0,temp=0;
        int priLen=prices.length;
        for(i=0;i<priLen-1;i++){
            for(j=i;j<priLen;j++){
                temp=prices[j]-prices[i];
                if(max<temp){
                    max=temp;
                }
            }
        }
        if(max<=0){
            return 0;
        }
        return max;
    }*/


    //好好学习思路
    public int maxProfit(int[] prices) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice) {
                minprice = prices[i];
            } else if (prices[i] - minprice > maxprofit) {
                maxprofit = prices[i] - minprice;
            }
        }
        return maxprofit;
    }


}
